# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openexr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.0
pkgrel=2
pkgdesc="Openexr library for EXR images (mingw-w64)"
arch=('any')
url="http://www.openexr.com/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"  "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-ilmbase" "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=("http://download.savannah.nongnu.org/releases/openexr/${_realname}-${pkgver}.tar.gz"
        mingw-w64-fix.patch
        openexr-2.1.0-headers.patch
        openexr-2.1.0_bb44ExpLogTable.patch
        openexr-2.1.0_aligned-malloc.patch
        openexr-2.1.0_cast.patch
        openexr_obsolete-macros.patch
        cmake-soversion.patch
        pull93.patch
        change-suffixes-for-64bit-literals.patch
        win-dwalookups.patch)
sha256sums=('36a012f6c43213f840ce29a8b182700f6cf6b214bea0d5735594136b44914231'
            'becdb62e22589fec7baa90690f4751869fa97f3c4af0cfc022f37ad371d2509f'
            '461c2a79b25739d94187f7c71747ca1f019ecd359fc5fa5fe20f71b4fa18f764'
            'eb7bafee7ad5d2bae240184347407c408ebdc875f93f619d5bd9beb6fef65aeb'
            'ce0aa05382dbd9c76d350ce8f5e56a4f62dfad3f241004cf27e2e6e3ecc77f17'
            'af9f6389d1994de72e9ba2249e81603fb54d3d879feb9d1144328268c9bf4714'
            '60d239264166803a18de10ffd62cbd6db6a7fb0ace6c461e16672a8e3b2f4f04'
            'a89ac2fcc5408dc921bf932451974848ba70428f3e4033ee378bbbfa580a65ce'
            '38297ab67d9cc2c6b2a8799106a30e14577db8c7d0d62d1681273f2e4c75ea7b'
            'bea5063424000076a1f202c80c821e71dfb814db5c10f834ff7b1836317a31a0'
            '201d05b2eac8ca74c796012584a800e78aa49919970b50399f37d4be8c83c0fc')

prepare(){
  cd "${srcdir}/${_realname}-$pkgver"

  sed -i 's/#define ZLIB_WINAPI/\/\/#define ZLIB_WINAPI/g' IlmImf/ImfZipCompressor.cpp
  sed -i 's/#define ZLIB_WINAPI/\/\/#define ZLIB_WINAPI/g' IlmImf/ImfPxr24Compressor.cpp

  patch -p1 -i "${srcdir}/mingw-w64-fix.patch"
  patch -p1 -i "${srcdir}/openexr-2.1.0-headers.patch"
  #patch -p1 -i "${srcdir}/openexr-2.1.0_bb44ExpLogTable.patch"
  patch -p1 -i "${srcdir}/openexr-2.1.0_aligned-malloc.patch"
  patch -p1 -i "${srcdir}/openexr-2.1.0_cast.patch"
  patch -p1 -i "${srcdir}/openexr_obsolete-macros.patch"
  patch -p1 -i "${srcdir}/cmake-soversion.patch"
  patch -p2 -i "${srcdir}/pull93.patch"
  patch -p2 -i ${srcdir}/change-suffixes-for-64bit-literals.patch
  patch -p1 -i ${srcdir}/win-dwalookups.patch

}

build() {
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/OpenEXR"
  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DNAMESPACE_VERSIONING=ON \
    -DUSE_ZLIB_WINAPI=OFF \
    ../${_realname}-${pkgver}

  #-DCMAKE_VERBOSE_MAKEFILE=ON
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
